import { Injectable } from '@angular/core';
import { HttpClient } from '../../../../providers/httpClient';
import { Config } from '../../../../providers/config';
import { Storage } from '@ionic/storage';
import _ from 'lodash';

@Injectable()
export class MarketingWeeklyEditService {
	constructor(
		public httpClient: HttpClient,
		public config: Config,
		public storage: Storage,
		) { }
	/**
	 * [getChanelListData 获取工作日报]
	 * @param {[type]} weeklyId [工作周报id]
	 */
	 getChanelListData(weeklyId) {
	 	let postBody = {
	 		"module": "KSB",
	 		"method": "USP_A02_0090",
	 		"type":"USP",
	 		"params": {
	 			"workweeeklyid":weeklyId,
	 			"usr_id":""
	 		}
	 	};
	 	console.log("#####工作周报工作日报查询条件######");
	 	console.log(postBody);
	 	return this.httpClient.post(postBody);
	 }
	/**
	 * [saveWeekly 保存工作周报]
	 * @param {[type]} editmode [编辑模式，0：新增，1：修改]
	 * @param {[type]} item [工作周报对象]
	 * @param {[type]} userInfo [当前登录用户信息]
	 */
	 saveWeekly(indexAuthority,editmode,item,userInfo){
	 	//根据用户类型确定
	 	let weekly = {
	 		"C_COMMUREC_type_i":indexAuthority,
	 		"WORKWEEKLYID_i" : item[0],
	 		"BEGINDATE_i":item[1],
	 		"ENDDATE_i":item[2],
	 		"login_nm_i":userInfo.loginid,
	 		"TITLE_i":item[4],
	 		"SUMMARY_i":item[5] ? item[5]:"",
	 		"VC_PLAN_i":item[6] ? item[6]:"",
	 		"SUGGESTION_i":item[7] ? item[7]:"",
	 		"REMARK_i":"",
	 		"COMMURECIDS_i":item[8] ? item[8]:""
	 	}
	 	console.log(weekly);
	 	//新增
	 	if(editmode == 0){
	 		weekly["WORKWEEKLYID_i"] = 0;
	 	}
	 	let postBody = {
	 		"module": "KSB",
	 		"method": "USP_A03_0010",
	 		"type":"USP",
	 		"params": weekly
	 	};
	 	console.log("#####保存工作周报业务对象######");
	 	console.log(postBody);
	 	return this.httpClient.post(postBody,{ isShowLoading: true});
	 }
	 	 /*
	 *[getWeekOfMonth 计算当前日期在本月份的周数]
	 * @param {[type]} dateString [日期字符串：20170505]
	 */
	 getWeekOfMonth(dateString,weekStart = 0) {
	 	dateString = dateString.replace(/-/g,'');
	 	var y = dateString.substr(0,4);
	 	var m = dateString.substr(4,2);
	 	var d = dateString.substr(6,2);
	 	var date = new Date(y, m -1, d); // 注意：JS 中月的取值范围为 0~11
     console.log("getWeekOfMonth.y", y);
     console.log("getWeekOfMonth.m", m);
     console.log("getWeekOfMonth.d", d);
     console.log("getWeekOfMonth.date", date);

	 	weekStart = (weekStart || 0) - 0;
	 	if (isNaN(weekStart) || weekStart > 6)
	 		weekStart = 0;

	 	var dayOfWeek = date.getDay();
     console.log("getWeekOfMonth.dayOfWeek", dayOfWeek);
	 	var day = date.getDate();
     console.log("getWeekOfMonth.day", day);
	 	return Math.ceil((day - dayOfWeek - 1) / 7) + ((dayOfWeek >= weekStart) ? 1 : 0);
	 }
	 /*
	 * 获取当前日期周几
	 *@param {[type]} days [周几：比如周一就是1，周五就是5]
	 */
	 getFirstDayOfWeek (days) {
	 	var date =  new Date();
	 	var day = date.getDay() || 7;
	 	var newdate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + days - day);
	 	let m = newdate.getMonth() + 1;
	 	let d = newdate.getDate();
	 	var ms = m < 10 ? '0' + m.toString() : m;
	 	var ds = d < 10 ? '0' + d.toString() : d;
	 	return newdate.getFullYear() +"-" + ms + "-" + ds;
	 }

	 /*
	 * 获取给定日期所在周的日期数组
	 *@param {[type]} days [周几：比如周一就是1，周五就是5]
	 */
	 getCurrentWeek(dateString) {

	 	var y = dateString.substr(0,4);
	 	// var m = dateString.substr(4,2).replace("0", "");
	 	var m = dateString.substr(4,2);
	 	var d = dateString.substr(6,2);
	 	var date = new Date(y, m -1, d); // 注意：JS 中月的取值范围为 0~11

	 	//起止日期数组
	 	var startStop = new Array();
	 	//获取当前时间
	 	var currentDate = date;
	 	//返回date是一周中的某一天
	 	var week = currentDate.getDay();
	 	//返回date是一个月中的某一天
	 	var month = currentDate.getDate();

	 	//一天的毫秒数
	 	var millisecond = 1000 * 60 * 60 * 24;
	 	//减去的天数
	 	var minusDay = week != 0 ? week - 1 : 6;
	 	//alert(minusDay);
	 	//本周 周一
	 	var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
	 	//本周 周二
	 	var d2 = new Date(monday.getTime() + (1 * millisecond));
	 	//本周 周三
	 	var d3 = new Date(monday.getTime() + (2 * millisecond));
	 	//本周 周四
	 	var d4 = new Date(monday.getTime() + (3 * millisecond));
	 	//本周 周五
	 	var d5 = new Date(monday.getTime() + (4 * millisecond));
	 	//本周 周六
	 	var d6 = new Date(monday.getTime() + (5 * millisecond));
	 	//本周 周日
	 	var sunday = new Date(monday.getTime() + (6 * millisecond));
	 	//添加本周时间
	 	startStop.push(this.formatDate(monday).replace(/-/g,'')); //本周起始时间

	 	startStop.push(this.formatDate(d2).replace(/-/g,'')); //本周起始时间
	 	startStop.push(this.formatDate(d3).replace(/-/g,'')); //本周起始时间
	 	startStop.push(this.formatDate(d4).replace(/-/g,'')); //本周起始时间
	 	startStop.push(this.formatDate(d5).replace(/-/g,'')); //本周起始时间
	 	startStop.push(this.formatDate(d6).replace(/-/g,'')); //本周起始时间
	 	//添加本周最后一天时间
	 	startStop.push(this.formatDate(sunday).replace(/-/g,'')); //本周终止时间
	 	//返回
	 	return startStop;
	 }

	 formatDate(date) {
	 	var y = date.getFullYear();
	 	var m = date.getMonth() + 1;
	 	m = m < 10 ? '0' + m : m;
	 	var d = date.getDate();
	 	d = d < 10 ? ('0' + d) : d;
	 	return y + '-' + m + '-' + d;
	 }

	}
